import { Menu } from 'antd';
import React from 'react';
import {Link} from "react-router-dom";
import {PRIVATE_ROUTE} from "../../route.constants";
const { SubMenu } = Menu;

 export  default class SideBarNavigationMenu extends React.Component {
    state = {
        theme: 'dark',
        current: '1',
    };

    changeTheme = value => {
        this.setState({
            theme: value ? 'dark' : 'light',
        });
    };

    handleClick = e => {
        this.setState({
            current: e.key,
        });
    };


    createSubMenu=(data)=>{
        return (data || []).map(item=>{

            if(item.parentId==0){
                return [
                    <SubMenu key={item.key} title={item.title}>
                        {this.createSubMenu(item.children)}
                    </SubMenu>
                ]
            }else{

                if(item.children.length==0){
                    return [<Menu.Item key={item.key}>
                        <Link to={'/main/'+PRIVATE_ROUTE[item.code]}>{item.title}</Link>
                    </Menu.Item>]
                }else{
                    return [
                        <SubMenu key={item.key}  title={item.title}>
                            {this.createSubMenu(item.children)}
                        </SubMenu>
                    ]

                }

            }

        })

    }

     render() {
         return (
             <>
                 <Menu
                     theme={this.state.theme}
                     onClick={this.handleClick}
                     style={{ width: 256 }}
                     selectedKeys={[this.state.current]}
                     mode="inline"
                 >
                     {this.createSubMenu(this.props.data)}

                 </Menu>
             </>
         );
     }
}
